package com.jerry.walking.module.activityPath.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jerry.walking.R;
import com.jerry.walking.base.BaseActivity;
import com.jerry.walking.base.BaseApplication;
import com.jerry.walking.constant.Urls;
import com.jerry.walking.module.activityPath.bean.PathSpecial3;
import com.jerry.walking.module.activityPath.bean.SpecialSecond2;
import com.jerry.walking.module.activityPath.bean.SpecicalSecond1;
import com.jerry.walking.module.activityTravel.activity.BigPicture;
import com.jerry.walking.utils.OkHttpUtils;
import com.jerry.walking.views.ExpandListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yang on 2016/5/14.
 * <p/>
 * 在路线专题中的点击事件显示列表的点击事件
 * <p/>
 * SpecialFirst  ---->   SpecialSecond
 */
public class SpecialSecond extends BaseActivity {

    @BindView(R.id.text3)
    TextView textView; // bar

    @BindView(R.id.iv_share_second)
    ImageView imageView; // 分享

    @BindView(R.id.tv_path_special_second6)
    TextView textView1; // viewpager中的数字

    @BindView(R.id.tv_path_special_second1)
    TextView textView2; // 名字

    @BindView(R.id.tv_path_special_second2)
    TextView textView3; // 地址

    @BindView(R.id.tv_path_special_second3)
    TextView textView4; // 主题内容

    @BindView(R.id.tv_path_special_second4)
    TextView textView5; // 讨论区数据

    @BindView(R.id.tv_path_special_second7)
    TextView textView6; // 发帖按钮

    @BindView(R.id.tv_path_special_second5)
    TextView textView7; // 相邻路线数据

    @BindView(R.id.lv_path_special_second)
    ExpandListView listView;

    @BindView(R.id.vp_path_special_second)
    ViewPager viewPager;    // 第一个

    @BindView(R.id.rl_path_special_second3)
    RelativeLayout relativeLayout;
    @BindView(R.id.vp_path_special_second1)
    ViewPager viewPager1;   // 第二个
    @BindView(R.id.iv_path_path_special_second1)
    ImageView imageView1; // 最后的小点

    // 第一个viewpager图集
    private List<SpecicalSecond1.PicturesBean> images;
    private PagerAdapter adapter;

    //第二个viewpager图集
    private List<PathSpecial3.TrailsBean> images1;
    private List<List<String>> picutes;
    private PagerAdapter adapter1;

    private int id = -1;
    private String name = "";

    //listview聊天室图集
    private List<SpecialSecond2.PostsBean> items;
    private int page = 1; // 聊天室的页数

    private ImageLoader imageLoader;

    @Override
    protected int getLayoutId(){
        id = Integer.parseInt(getIntent().getStringExtra("id"));
        name = getIntent().getStringExtra("name");
        return R.layout.activity_path_special_second;
    }

    @Override
    protected void initViews() {
        super.initViews();

        textView.setText(name);

        imageLoader = BaseApplication.getApplication().getImageLoader();

        //第一个viewpager适配及滑动监听
        images = new ArrayList<>();
        adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return images.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(SpecialSecond.this);
                DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .cacheOnDisk(true)
                        .cacheInMemory(true)
                        .imageScaleType(ImageScaleType.EXACTLY)
                        .build();
                imageLoader.displayImage(images.get(position).getPicture(), imageView, options);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
//                super.destroyItem(container, position, object);
                container.removeView((View) object);
            }
        };
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                textView1.setText(position + 1 + "/" + images.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //讨论区listview的适配及监听
        items = new ArrayList<>();


        //第二个viewpager 推荐路线适配适配
        //并判断数据是否存在
        images1 = new ArrayList<>();
        picutes = new ArrayList<>();

        adapter1 = new PagerAdapter() {
            @Override
            public int getCount() {
                return picutes.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, final int index) {

                GridView gridView = new GridView(SpecialSecond.this);
                gridView.setNumColumns(3);
                gridView.setVerticalSpacing(5);
                gridView.setHorizontalSpacing(20);

                gridView.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return picutes.get(index).size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return picutes.get(index).get(position);
                    }

                    @Override
                    public long getItemId(int position) {
                        return position;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {

                        View inflate = View.inflate(SpecialSecond.this, R.layout.adapter_one_imageview, null);
                        ImageView imageview = (ImageView) inflate.findViewById(R.id.iv_one);
                        TextView textview = (TextView) inflate.findViewById(R.id.tv_two_iv);
                        imageview.setBackgroundResource(R.color.color_hui);
                        DisplayImageOptions options = new DisplayImageOptions.Builder()
                                .cacheOnDisk(true)
                                .cacheInMemory(true)
                                .imageScaleType(ImageScaleType.EXACTLY)
                                .build();
                        imageLoader.displayImage(picutes.get(index).get(position), imageview, options);
                        imageview.setScaleType(ImageView.ScaleType.FIT_XY);
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                        imageview.setLayoutParams(params);

                        textview.setText(images1.get(index*3+position).getName());

                        return inflate;
                    }
                });
                //监听器
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                        Intent intent = new Intent(SpecialSecond.this, SpecialSecond.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("id", images1.get(index*3+i).getId());
                        intent.putExtra("name", images1.get(index*3+i).getName());
                        startActivity(intent);
                    }
                });

                container.addView(gridView);
                return gridView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
//                super.destroyItem(container, position, object);
                container.removeView((View) object);
            }
        };
        viewPager1.setAdapter(adapter1);

    }

    @Override
    protected void loadData() {
        super.loadData();

        //得到主内容下载
        OkHttpUtils.get(Urls.PATH_ROUTEBASE_ITEM_DATA + id + Urls.PATH_ROUTEBASE_ITEM_DATA_CURRENT + id + Urls.PATH_ROUTEBASE_ITEM_DATA_END, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                Gson gson = new Gson();
                final SpecicalSecond1 specicalSecond1 = gson.fromJson(data, SpecicalSecond1.class);
                final List<SpecicalSecond1.PicturesBean> pictures = specicalSecond1.getPictures();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //基本数据填充
                        textView2.setText(specicalSecond1.getName());
                        textView3.setText(specicalSecond1.getDestination());

                        textView4.setText(specicalSecond1.getDetail());
                        textView5.setText("全部" + specicalSecond1.getDiscussion_bbs_count() + "条");

                        //设置图片数量
                        textView1.setText("1/" + pictures.size());
                    }
                });

                images.addAll(pictures);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });

        //讨论组下载
        OkHttpUtils.get(Urls.PATH_ROUTEBASE_ITEM_DISCUSSION + id + Urls.PATH_ROUTEBASE_ITEM_DISCUSSION_CURRENT + id + Urls.PATH_ROUTEBASE_ITEM_DISCUSSION_END + page,
                new OkHttpUtils.OnGetDataListener() {
                    @Override
                    public void onResponse(String url, String data) {
                        Gson gson = new Gson();
                        SpecialSecond2 specialSecond2 = gson.fromJson(data, SpecialSecond2.class);
                        List<SpecialSecond2.PostsBean> posts = specialSecond2.getPosts();

                        

                        items.addAll(posts);
                    }

                    @Override
                    public void onFailure(String url, String error) {

                    }
                });

        //相邻路线下载
        OkHttpUtils.get(Urls.PATH_ROUTEBASE_ITEM_ADJACENT_ROUTE + id + Urls.PATH_ROUTEBASE_ITEM_ADJACENT_ROUTE_CURRENT + id + Urls.PATH_ROUTEBASE_ITEM_ADJACENT_ROUTE_END
                , new OkHttpUtils.OnGetDataListener() {
                    @Override
                    public void onResponse(String url, String data) {
                        Gson gson = new Gson();

                        PathSpecial3 pathSpecial3 = gson.fromJson(data, PathSpecial3.class);

                        List<PathSpecial3.TrailsBean> trails = pathSpecial3.getTrails();

                        if (trails.size() == 0) {
                            relativeLayout.setVisibility(View.GONE);
                        } else {
                            relativeLayout.setVisibility(View.VISIBLE);
                            List<List<String>> lists = new ArrayList<>();
                            int j = 0;
                            for (int i = 0; i < trails.size() / 3+(trails.size()%3==0?0:1); i++) {
                                List<String> list = new ArrayList<>();
                                for (; (j + 1) % 4 != 0; j++) {
                                    list.add(trails.get(j).getCover());
                                }
                                j++;
                                lists.add(list);
                            }
                            picutes.addAll(lists);
                            images1.addAll(trails);
                            adapter1.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFailure(String url, String error) {

                    }
                });
    }
}